// pages/02admin/03table/table.js
var app = getApp()
var appData = app.globalData
var i,j
Page({
    data: {
        tableList: null,
        cate:null
    },

    onLoad: function (options) {
        var that = this
        var param = { uid: appData.shopInfo.shopInfo.userid }
        app.getJsonData('api/space/table_list_admin.php', param, function (res) {
            that.handleTableListData(res)
        })

        app.setNoticeTableListAdmin(function (updateData) {
            console.log(updateData)
            console.log('updateData')
            var u = updateData
            var arr = that.data.tableList
            if (u.sql == 'insert'){
                arr[u.pick_i].list.push(u.table)
                console.log(arr[u.pick_i].list)
                console.log('arr[u.pick_i].list')
            } else if (u.sql == 'update') {
                arr[i].list[j] = u.table
                console.log(arr[i].list)
                console.log('arr[i].list')
            }
        
            that.setData({
                tableList: arr
            })
        })
    },

    handleTableListData: function (res) {
        if (res.code == 0) {
            var t_list = res.data
            var cate = {
                idArr:[],
                nameArr:[]
            }
            for (var i = 0; i < t_list.length; i++) {
                cate.idArr.push(t_list[i].cateId)
                cate.nameArr.push(t_list[i].cateName)
            }
            this.setData({
                tableList: res.data,
                cate: cate
            })
        }
    },

    editTable: function (e) {
        if (app.globalData.loginMode == 'boss') {
        var e_id = e.currentTarget.id
        if(e_id){
            var e_ids = e_id.split(':');
            i = Number(e_ids[0])
            j = Number(e_ids[1])
            var tab = {
                table: this.data.tableList[i].list[j],
                cate: this.data.cate
            }
        }else{
            var tab = {
                table: null,
                cate: this.data.cate
            }
        }
        
        var tabStr = JSON.stringify(tab)
        wx.navigateTo({
            url: '/pages/02admin/03setTable/setTable?tab=' + tabStr
        })
        }
        else {
            wx.showToast({
                title: '需要掌柜登录才能操作',
            })
        }
    },

    delTable: function (e) {
        if (app.globalData.loginMode == 'boss') {
        var e_id = e.currentTarget.id
        var e_ids = e_id.split(':');
        i = Number(e_ids[0])
        j = Number(e_ids[1])
        var arr = this.data.tableList
        var sta = arr[i].list[j].status
        if (sta < 2){
            this.deleteTable(arr)
        }else{
            wx.showToast({
                title: '删除失败，空桌和关桌状态才能删除',
            })
        }
    }
    else{
        wx.showToast({
            title: '需要掌柜登录才能操作',
        })
    }
    },

     deleteTable:function(arr){
         if (app.globalData.loginMode == 'boss') {
        var param = {
            id: arr[i].list[j].id,
            sql: 'delete from'
        }
        var that = this
        app.getJsonData('api/admin/table_save.php', param, function (res) {
            console.log(res)
            console.log('res')
            if (res.code == 0) {
                arr[i].list.splice(j, 1)
                that.setData({
                    tableList: arr
                })
                app.noticeUpdateTableListAdmin(res.data)
            }
        })
    }
    else{
        wx.showToast({
            title: '需要掌柜登录才能操作',
        })
    }
    },
})